package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.MenuDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

@Service(interfaceClass = MenuService.class)
@Transactional
@Slf4j
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;

    @Override
    public void add(Menu menu) {
        menuDao.add(menu);
    }

    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {

            // 使用分页插件PageHelper
            PageHelper.startPage(currentPage,pageSize);
            // 获取分页数据
            Page<Menu> page = menuDao.selectByCondition(queryString);
            // 封装分页返回对象
            return new PageResult(page.getTotal(),page.getResult());

    }

    @Override
    public void edit(Menu menu) {
        menuDao.edit(menu);
    }

    @Override
    public Menu findById(Integer id) {
        return menuDao.findById(id);
    }

    @Override
    public void deleteById(Integer id) {
        //检查当前菜单项是否有角色数据
        Long count = menuDao.countMenuById(id);
        // 如果有，抛出异常包含异常信息的异常
        if (count>0){
            throw new RuntimeException("当前菜单项有数据，不能删除");
        }
        menuDao.deleteMenuById(id);
    }
}
